<?php

class MyGraph extends Controller {

	// Constructor
	function MyGraph()
    {
        parent::Controller();
		
		// load model
		$this->load->model('Df602_model', 'df602', TRUE);
		$this->load->model('Graph_model', 'Graph', TRUE);
		
		// select table to scaff
		$this->load->scaffolding('realtime');
		
		// load session lib
		$this->load->library('session');
		
		
    }
	
	function retrieve()
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		$this->Graph->inMonth(1, "/dev/hda1", 300);
		/* echo "HOUR " ;
		$t1 = time() ; 
		$data = $this->Graph->inHour(1, "/dev/hda1", 300);
		$t2 = time() ;
		var_dump($data);
		
		echo "time :".($t2-$t1). "<br>" ;
		$t1 = time() ; 
		$data = $this->Graph->inDay(1, "/dev/hda1", 300);
		$t2 = time() ;
		var_dump($data);
		echo "time :".($t2-$t1). "<br>" ;
		$t1 = time() ; 
		$data = $this->Graph->inWeek(1, "/dev/hda1", 300);
		$t2 = time() ;
		var_dump($data);
		echo "time :".($t2-$t1). "<br>" ;
		$t1 = time() ; 
		$data = $this->Graph->inMonth(1, "/dev/hda1", 300);
		$t2 = time() ;
		var_dump($data);
		echo "time :".($t2-$t1). "<br>" ; */
	}
	
	function genHourGraph($clientID, $deviceName)
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		include ("./jpgraph/src/jpgraph.php");
		include ("./jpgraph/src/jpgraph_bar.php");
		require_once("./jpgraph/src/jpgraph_date.php"); 
		require_once("./jpgraph/src/jpgraph_line.php");
		
		$deviceName = str_replace("_", "/", $deviceName);
		$dataArrayForPlottingGraph = $this->Graph->inHour($clientID, $deviceName,300);

		// We need some data
		$start = time();
		$lastHour  = mktime(date("H")-1, date("i"), date("s"), date("m")  , date("d"), date("Y"));
		$datax = array();
		$datay = array();
		for($i=0; $i<300; $i++) 
		{
			$datay[]=rand(40,50);
			$datax[]=$lastHour+($i*60*60/300);
		}

		// Setup the graph. 
		$graph = new Graph(305,80,"auto");	
		$graph->SetScale("datlin", 0, 100);
		$graph->xaxis->scale->SetDateFormat(' G:i');
		$graph->xaxis->scale->SetTimeAlign(MINADJ_10,MINADJ_1);
		//$graph->xaxis->HideTicks();
		$graph->yaxis->HideLabels();
		$graph->yscale->ticks->Set(20);
		$graph->xscale->ticks->Set(600);
		$graph->img->SetMargin(10,10,15,20);
		$graph->SetAlphaBlending(true); 


		//$graph->title->Set('"GRAD_MIDVER"');
		$graph->title->SetColor('darkred');

		// Setup font for axis
		$graph->xaxis->SetFont(FF_FONT1);
		$graph->yaxis->SetFont(FF_FONT1);

		//$graph->xaxis->SetLabelAngle(90);

		// Create the bar pot
		$bplot = new BarPlot($dataArrayForPlottingGraph, $datax);
		$bplot->SetAbsWidth(0.00001);

		// Setup color for gradient fill style 
		//$bplot->SetFillGradient("navy","lightsteelblue",GRAD_MIDVER);

		// Set color for the frame of each bar
		$bplot->SetColor("red@0.6");
		$bplot->SetNoFill(); 
		$bplot->SetFillColor('red@0.5');
		$color = array(0, 0, 0);
		$graph->SetFrame(false); 
		$graph->Add($bplot);

		// Finally send the graph to the browser
		$graph->Stroke();
	}
	
	function genDayGraph($clientID, $deviceName)
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		include ("./jpgraph/src/jpgraph.php");
		include ("./jpgraph/src/jpgraph_bar.php");
		require_once("./jpgraph/src/jpgraph_date.php"); 
		require_once("./jpgraph/src/jpgraph_line.php");
		
		$deviceName = str_replace("_", "/", $deviceName);
		$dataArrayForPlottingGraph = $this->Graph->inDay($clientID, $deviceName,300);

		// We need some data
		$start = time();
		$yesterday  = mktime(date("H"), date("i"), date("s"), date("m")  , date("d")-1, date("Y"));
		$datax = array();
		$datay = array();
		for($i=0; $i<300; $i++) 
		{
			$datay[]=rand(40,50);
			$datax[]=$yesterday+($i*24*60*60/300);
		}

		// Setup the graph. 
		$graph = new Graph(305,80,"auto");	
		$graph->SetScale("datlin", 0, 100);
		$graph->xaxis->scale->SetDateFormat('j');
		$graph->xaxis->scale->SetTimeAlign(HOURADJ_1,HOURADJ_1);
		//$graph->xaxis->HideTicks();
		$graph->yaxis->HideLabels();
		$graph->yscale->ticks->Set(25);
		$graph->xscale->ticks->Set(3600*2);
		$graph->img->SetMargin(10,10,15,20);
		$graph->SetAlphaBlending(true); 
	
		$graph->xaxis->SetTextLabelInterval(12); 
		//$graph->title->Set('"GRAD_MIDVER"');
		$graph->title->SetColor('darkred');

		// Setup font for axis
		$graph->xaxis->SetFont(FF_FONT1);
		$graph->yaxis->SetFont(FF_FONT1);

		//$graph->xaxis->SetLabelAngle(90);

		// Create the bar pot
		$bplot = new BarPlot($dataArrayForPlottingGraph, $datax);
		$bplot->SetAbsWidth(0.00001);

		// Setup color for gradient fill style 
		//$bplot->SetFillGradient("navy","lightsteelblue",GRAD_MIDVER);

		// Set color for the frame of each bar
		$bplot->SetColor("red@0.6");
		$bplot->SetNoFill(); 
		$bplot->SetFillColor('red@0.5');
		$color = array(0, 0, 0);
		$graph->SetFrame(false); 
		$graph->Add($bplot);

		// Finally send the graph to the browser
		$graph->Stroke();
	}
	
	function genWeekGraph($clientID, $deviceName)
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		include ("./jpgraph/src/jpgraph.php");
		include ("./jpgraph/src/jpgraph_bar.php");
		require_once("./jpgraph/src/jpgraph_date.php"); 
		require_once("./jpgraph/src/jpgraph_line.php");
		
		$deviceName = str_replace("_", "/", $deviceName);
		$dataArrayForPlottingGraph = $this->Graph->inWeek($clientID, $deviceName,300);

		// We need some data
		$start = time();
		$lastweek  = mktime(date("H"), date("i"), date("s"), date("m")  , date("d")-7, date("Y"));
		$datax = array();
		$datay = array();
		for($i=0; $i<300; $i++) 
		{
			$datay[]=rand(40,50);
			$datax[]=$lastweek+($i*24*7*60*60/300);
		}

		// Setup the graph. 
		$graph = new Graph(305,80,"auto");	
		$graph->SetScale("datlin", 0, 100);
		$graph->xaxis->scale->SetDateFormat('D');
		$graph->xaxis->scale->SetDateAlign(DAYADJ_1,DAYADJ_1);
		//$graph->xaxis->HideTicks();
		$graph->yaxis->HideLabels();
		$graph->yscale->ticks->Set(25);
		$graph->xscale->ticks->Set(3600*24);
		$graph->img->SetMargin(10,10,15,20);
		$graph->SetAlphaBlending(true); 
		
		$graph->xaxis->SetTextLabelInterval(2); 

		//$graph->title->Set('"GRAD_MIDVER"');
		$graph->title->SetColor('darkred');

		// Setup font for axis
		$graph->xaxis->SetFont(FF_FONT1);
		$graph->yaxis->SetFont(FF_FONT1);

		//$graph->xaxis->SetLabelAngle(90);

		// Create the bar pot
		$bplot = new BarPlot($dataArrayForPlottingGraph, $datax);
		$bplot->SetAbsWidth(0.00001);

		// Setup color for gradient fill style 
		//$bplot->SetFillGradient("navy","lightsteelblue",GRAD_MIDVER);

		// Set color for the frame of each bar
		$bplot->SetColor("red@0.6");
		$bplot->SetNoFill(); 
		$bplot->SetFillColor('red@0.5');
		$color = array(0, 0, 0);
		$graph->SetFrame(false); 
		$graph->Add($bplot);

		// Finally send the graph to the browser
		$graph->Stroke();
	}
	
	function genMonthGraph($clientID, $deviceName)
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		include ("./jpgraph/src/jpgraph.php");
		include ("./jpgraph/src/jpgraph_bar.php");
		require_once("./jpgraph/src/jpgraph_date.php"); 
		require_once("./jpgraph/src/jpgraph_line.php");
		
		$deviceName = str_replace("_", "/", $deviceName);
		$dataArrayForPlottingGraph = $this->Graph->inMonth($clientID, $deviceName,300);

		// We need some data
		$start = time();
		$lastMonth  = mktime(date("H"), date("i"), date("s"), date("m")-1  , date("d"), date("Y"));
		$datax = array();
		$datay = array();
		for($i=0; $i<300; $i++) 
		{
			$datay[]=rand(40,50);
			$datax[]=$lastMonth+($i*24*60*60*30/300);
		}

		// Setup the graph. 
		$graph = new Graph(305,80,"auto");	
		$graph->SetScale("datlin", 0, 100);
		$graph->xaxis->scale->SetDateFormat('M');
		$graph->xaxis->scale->SetDateAlign(DAYADJ_1,DAYADJ_1);
		//$graph->xaxis->HideTicks();
		$graph->yaxis->HideLabels();
		$graph->yscale->ticks->Set(25);
		$graph->xscale->ticks->Set(3600*24*30);
		$graph->img->SetMargin(10,10,15,22);
		$graph->SetAlphaBlending(true); 
		
		//$graph->xaxis->SetTextLabelInterval(5); 

		//$graph->title->Set('"GRAD_MIDVER"');
		$graph->title->SetColor('darkred');

		// Setup font for axis
		$graph->xaxis->SetFont(FF_FONT1);
		$graph->yaxis->SetFont(FF_FONT1);

		//$graph->xaxis->SetLabelAngle(90);

		// Create the bar pot
		$bplot = new BarPlot($dataArrayForPlottingGraph, $datax);
		$bplot->SetAbsWidth(0.00001);

		// Setup color for gradient fill style 
		//$bplot->SetFillGradient("navy","lightsteelblue",GRAD_MIDVER);

		// Set color for the frame of each bar
		$bplot->SetColor("red@0.6");
		$bplot->SetNoFill(); 
		$bplot->SetFillColor('red@0.5');
		$color = array(0, 0, 0);
		$graph->SetFrame(false); 
		$graph->Add($bplot);

		// Finally send the graph to the browser
		$graph->Stroke();
	}

}











?>